//
//  CZCZEmoticonPageViewCell.swift
//  CZVVeiBo
//
//  Created by smile on 2017/8/6.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

class CZCZEmoticonPageViewCell: UICollectionViewCell {
    
    // 保存20个按钮
    var buttonList:[CZEmoticonButton] = [CZEmoticonButton]()
    
    // 定义一个一维数组供外界赋值
    var emoticons:[CZEmoticonModel]?{
        didSet{
            
            // 20个按钮全部隐藏
            for button in buttonList{
                button.isHidden = true
            }
            

            for (i, model) in emoticons!.enumerated(){
                // 通过下标获取对应按钮
                let button = buttonList[i]
                // 模型赋值
                button.emoticonModel = model
                
                // 显示按钮
                button.isHidden = false
                // 判断是图片表情还是emoji
                // emoji
                if model.isEmoji {
                    // 
                    let code = model.code ?? ""
                    // 转emoji表情
                    let codeStr = (code as NSString).emoji()
                    button.setTitle(codeStr, for: UIControlState.normal)
                    button.setImage(nil, for: UIControlState.normal)
                    
                }else{
                   // 图片表情
                    let png = model.allPathPng ?? ""
                    let image = UIImage(named: png, in: CZEmoticonTools.share.emoticonBundle, compatibleWith: nil)
                    button.setImage(image, for: UIControlState.normal)
                    button.setTitle(nil, for: UIControlState.normal)
                }
            }
        }
    }
    
    var indexPath: IndexPath?{
        didSet{
            
//            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func buttonClick(button: CZEmoticonButton){
        // 发送通知
        NotificationCenter.default.post(name: CZEMOTICONBUTTONSNOTI, object: button.emoticonModel)
    }
    
    @objc private func deleteButtonClick(){
        NotificationCenter.default.post(name: CZEMOTICONDELETEBUTTONNOTI, object: nil)
    }
    
    private func setupUI(){
        addChildButtons()
        contentView.addSubview(messageLabel)
        contentView.addSubview(deleteButton)
        
        messageLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
    }
    
    // 创建20个按钮
    private func addChildButtons(){
        for _ in 0..<EMOTICONMAXCOUNT{
            let button = CZEmoticonButton()
            // 监听事件
            button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
            // 保存按钮
            buttonList.append(button)
            contentView.addSubview(button)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 计算按钮的宽和高
        let buttonW = self.frame.width / CGFloat(EMOTICONMAXCOL)
        let buttonH = self.frame.height / CGFloat(EMOTICONMAXROW)
        // 设置frame
        for (i, button) in buttonList.enumerated(){
            
            // 计算列和行索引
            let col = CGFloat(i%EMOTICONMAXCOL)
            let row = CGFloat(i/EMOTICONMAXCOL)
            button.frame = CGRect(x: buttonW*col, y: buttonH*row, width: buttonW, height: buttonH)
        }
        
        deleteButton.frame = CGRect(x: self.frame.width - buttonW, y: buttonH*2, width: buttonW, height: buttonH)
    }
    
    // MARK: - 懒加载
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.red, fontSize: 32)
        lab.numberOfLines = 0
        return lab
    }()
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(deleteButtonClick), for: UIControlEvents.touchUpInside)
        btn.setImage(UIImage(named:"compose_emotion_delete"), for: UIControlState.normal)
        btn.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
        return btn
    }()
}
